using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;


namespace ASPNETVN.PORTAL.Modules.FAQ.Components
{
	public partial class Item
	{
		#region Private members.
		
		protected int _ID;
		protected int _CategoryID;
		protected string _Title = string.Empty;
		protected string _QuesContent = string.Empty;
		protected string _Name = string.Empty;
		protected string _Email = string.Empty;
		protected DateTime _QuesDate = new DateTime(1753, 1, 1);
		protected string _ApprovedBy = string.Empty;
		protected DateTime _ApprovedDate = new DateTime(1753, 1, 1);
		protected string _AnwserContent = string.Empty;
		protected string _AnwserBy = string.Empty;
		protected DateTime _AnwserDate = new DateTime(1753, 1, 1);
		protected int _isAnswer;
		protected int _Status;
		protected DateTime _PublishedDate = new DateTime(1753, 1, 1);
		protected DateTime _ExpiredDate = new DateTime(1753, 1, 1);

		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
		public int ID
		{
			set {this._ID = value;}
			get {return this._ID;}
		}
		
		public int CategoryID
		{
			set {this._CategoryID = value;}
			get {return this._CategoryID;}
		}
		
		public string Title
		{
			set {this._Title = value;}
			get {return this._Title;}
		}
		
		public string QuesContent
		{
			set {this._QuesContent = value;}
			get {return this._QuesContent;}
		}
		
		public string Name
		{
			set {this._Name = value;}
			get {return this._Name;}
		}
		
		public string Email
		{
			set {this._Email = value;}
			get {return this._Email;}
		}
		
		public DateTime QuesDate
		{
			set {this._QuesDate = value;}
			get {return this._QuesDate;}
		}
		
		public string ApprovedBy
		{
			set {this._ApprovedBy = value;}
			get {return this._ApprovedBy;}
		}
		
		public DateTime ApprovedDate
		{
			set {this._ApprovedDate = value;}
			get {return this._ApprovedDate;}
		}
		
		public string AnwserContent
		{
			set {this._AnwserContent = value;}
			get {return this._AnwserContent;}
		}
		
		public string AnwserBy
		{
			set {this._AnwserBy = value;}
			get {return this._AnwserBy;}
		}
		
		public DateTime AnwserDate
		{
			set {this._AnwserDate = value;}
			get {return this._AnwserDate;}
		}
		
		public int isAnswer
		{
			set {this._isAnswer = value;}
			get {return this._isAnswer;}
		}
		
		public int Status
		{
			set {this._Status = value;}
			get {return this._Status;}
		}
		
		public DateTime PublishedDate
		{
			set {this._PublishedDate = value;}
			get {return this._PublishedDate;}
		}
		
		public DateTime ExpiredDate
		{
			set {this._ExpiredDate = value;}
			get {return this._ExpiredDate;}
		}
		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static Item Load(int id)
		{
			const string spName = "[dbo].[p_FAQ_Item_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, id);
			Item entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				entity = new Item();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
				if (!reader.IsDBNull(reader.GetOrdinal("QuesContent"))) entity.QuesContent = reader.GetString(reader.GetOrdinal("QuesContent"));
				if (!reader.IsDBNull(reader.GetOrdinal("Name"))) entity.Name = reader.GetString(reader.GetOrdinal("Name"));
				if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
				if (!reader.IsDBNull(reader.GetOrdinal("QuesDate"))) entity.QuesDate = reader.GetDateTime(reader.GetOrdinal("QuesDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedBy"))) entity.ApprovedBy = reader.GetString(reader.GetOrdinal("ApprovedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedDate"))) entity.ApprovedDate = reader.GetDateTime(reader.GetOrdinal("ApprovedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserContent"))) entity.AnwserContent = reader.GetString(reader.GetOrdinal("AnwserContent"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserBy"))) entity.AnwserBy = reader.GetString(reader.GetOrdinal("AnwserBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserDate"))) entity.AnwserDate = reader.GetDateTime(reader.GetOrdinal("AnwserDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("isAnswer"))) entity.isAnswer = reader.GetInt32(reader.GetOrdinal("isAnswer"));
				if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetInt32(reader.GetOrdinal("Status"));
				if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ExpiredDate"))) entity.ExpiredDate = reader.GetDateTime(reader.GetOrdinal("ExpiredDate"));
			}
			reader.Close();
			return entity;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static List<Item> SelectCollectionAll()
		{
			List<Item> collection = new List<Item>();
			SqlDataReader reader = (SqlDataReader) SelectReaderAll();
			while (reader.Read())
			{
				Item entity = new Item();
				
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
				if (!reader.IsDBNull(reader.GetOrdinal("QuesContent"))) entity.QuesContent = reader.GetString(reader.GetOrdinal("QuesContent"));
				if (!reader.IsDBNull(reader.GetOrdinal("Name"))) entity.Name = reader.GetString(reader.GetOrdinal("Name"));
				if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
				if (!reader.IsDBNull(reader.GetOrdinal("QuesDate"))) entity.QuesDate = reader.GetDateTime(reader.GetOrdinal("QuesDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedBy"))) entity.ApprovedBy = reader.GetString(reader.GetOrdinal("ApprovedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedDate"))) entity.ApprovedDate = reader.GetDateTime(reader.GetOrdinal("ApprovedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserContent"))) entity.AnwserContent = reader.GetString(reader.GetOrdinal("AnwserContent"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserBy"))) entity.AnwserBy = reader.GetString(reader.GetOrdinal("AnwserBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserDate"))) entity.AnwserDate = reader.GetDateTime(reader.GetOrdinal("AnwserDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("isAnswer"))) entity.isAnswer = reader.GetInt32(reader.GetOrdinal("isAnswer"));
				if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetInt32(reader.GetOrdinal("Status"));
				if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ExpiredDate"))) entity.ExpiredDate = reader.GetDateTime(reader.GetOrdinal("ExpiredDate"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static List<Item> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			List<Item> collection = new List<Item>();

			SqlDataReader reader = (SqlDataReader) SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				Item entity = new Item();
				
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
				if (!reader.IsDBNull(reader.GetOrdinal("QuesContent"))) entity.QuesContent = reader.GetString(reader.GetOrdinal("QuesContent"));
				if (!reader.IsDBNull(reader.GetOrdinal("Name"))) entity.Name = reader.GetString(reader.GetOrdinal("Name"));
				if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
				if (!reader.IsDBNull(reader.GetOrdinal("QuesDate"))) entity.QuesDate = reader.GetDateTime(reader.GetOrdinal("QuesDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedBy"))) entity.ApprovedBy = reader.GetString(reader.GetOrdinal("ApprovedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedDate"))) entity.ApprovedDate = reader.GetDateTime(reader.GetOrdinal("ApprovedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserContent"))) entity.AnwserContent = reader.GetString(reader.GetOrdinal("AnwserContent"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserBy"))) entity.AnwserBy = reader.GetString(reader.GetOrdinal("AnwserBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("AnwserDate"))) entity.AnwserDate = reader.GetDateTime(reader.GetOrdinal("AnwserDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("isAnswer"))) entity.isAnswer = reader.GetInt32(reader.GetOrdinal("isAnswer"));
				if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetInt32(reader.GetOrdinal("Status"));
				if (!reader.IsDBNull(reader.GetOrdinal("PublishedDate"))) entity.PublishedDate = reader.GetDateTime(reader.GetOrdinal("PublishedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ExpiredDate"))) entity.ExpiredDate = reader.GetDateTime(reader.GetOrdinal("ExpiredDate"));
				collection.Add(entity);
			}
			
			reader.Close();
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_FAQ_Item_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_FAQ_Item_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_FAQ_Item_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_FAQ_Item_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertItem(int categoryID, string title, string quesContent, string name, string email, DateTime quesDate, string approvedBy, DateTime approvedDate, string anwserContent, string anwserBy, DateTime anwserDate, int isAnswer, int status, DateTime publishedDate, DateTime expiredDate)
		{
			Item entity = new Item();	
			entity.CategoryID = categoryID;
			entity.Title = title;
			entity.QuesContent = quesContent;
			entity.Name = name;
			entity.Email = email;
			entity.QuesDate = quesDate;
			entity.ApprovedBy = approvedBy;
			entity.ApprovedDate = approvedDate;
			entity.AnwserContent = anwserContent;
			entity.AnwserBy = anwserBy;
			entity.AnwserDate = anwserDate;
			entity.isAnswer = isAnswer;
			entity.Status = status;
			entity.PublishedDate = publishedDate;
			entity.ExpiredDate = expiredDate;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_FAQ_Item_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
			db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
			db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, Title);
			db.AddInParameter(dbCommand, "@QuesContent", SqlDbType.NVarChar, QuesContent);
			db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, Name);
			db.AddInParameter(dbCommand, "@Email", SqlDbType.NVarChar, Email);
			db.AddInParameter(dbCommand, "@QuesDate", SqlDbType.DateTime, QuesDate.Year <= 1753 ? DBNull.Value : (object) QuesDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.NVarChar, ApprovedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year <= 1753 ? DBNull.Value : (object) ApprovedDate);
			db.AddInParameter(dbCommand, "@AnwserContent", SqlDbType.NVarChar, AnwserContent);
			db.AddInParameter(dbCommand, "@AnwserBy", SqlDbType.NVarChar, AnwserBy);
			db.AddInParameter(dbCommand, "@AnwserDate", SqlDbType.DateTime, AnwserDate.Year <= 1753 ? DBNull.Value : (object) AnwserDate);
			db.AddInParameter(dbCommand, "@isAnswer", SqlDbType.Int, isAnswer);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.Int, Status);
			db.AddInParameter(dbCommand, "@PublishedDate", SqlDbType.DateTime, PublishedDate.Year <= 1753 ? DBNull.Value : (object) PublishedDate);
			db.AddInParameter(dbCommand, "@ExpiredDate", SqlDbType.DateTime, ExpiredDate.Year <= 1753 ? DBNull.Value : (object) ExpiredDate);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				ID = (int) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<Item> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Item item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateItem(int id, int categoryID, string title, string quesContent, string name, string email, DateTime quesDate, string approvedBy, DateTime approvedDate, string anwserContent, string anwserBy, DateTime anwserDate, int isAnswer, int status, DateTime publishedDate, DateTime expiredDate)
		{
			Item entity = new Item();			
			entity.ID = id;
			entity.CategoryID = categoryID;
			entity.Title = title;
			entity.QuesContent = quesContent;
			entity.Name = name;
			entity.Email = email;
			entity.QuesDate = quesDate;
			entity.ApprovedBy = approvedBy;
			entity.ApprovedDate = approvedDate;
			entity.AnwserContent = anwserContent;
			entity.AnwserBy = anwserBy;
			entity.AnwserDate = anwserDate;
			entity.isAnswer = isAnswer;
			entity.Status = status;
			entity.PublishedDate = publishedDate;
			entity.ExpiredDate = expiredDate;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_FAQ_Item_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
			db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, Title);
			db.AddInParameter(dbCommand, "@QuesContent", SqlDbType.NVarChar, QuesContent);
			db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, Name);
			db.AddInParameter(dbCommand, "@Email", SqlDbType.NVarChar, Email);
			db.AddInParameter(dbCommand, "@QuesDate", SqlDbType.DateTime, QuesDate.Year == 1753 ? DBNull.Value : (object) QuesDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.NVarChar, ApprovedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year == 1753 ? DBNull.Value : (object) ApprovedDate);
			db.AddInParameter(dbCommand, "@AnwserContent", SqlDbType.NVarChar, AnwserContent);
			db.AddInParameter(dbCommand, "@AnwserBy", SqlDbType.NVarChar, AnwserBy);
			db.AddInParameter(dbCommand, "@AnwserDate", SqlDbType.DateTime, AnwserDate.Year == 1753 ? DBNull.Value : (object) AnwserDate);
			db.AddInParameter(dbCommand, "@isAnswer", SqlDbType.Int, isAnswer);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.Int, Status);
			db.AddInParameter(dbCommand, "@PublishedDate", SqlDbType.DateTime, PublishedDate.Year == 1753 ? DBNull.Value : (object) PublishedDate);
			db.AddInParameter(dbCommand, "@ExpiredDate", SqlDbType.DateTime, ExpiredDate.Year == 1753 ? DBNull.Value : (object) ExpiredDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<Item> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Item item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateItem(int id, int categoryID, string title, string quesContent, string name, string email, DateTime quesDate, string approvedBy, DateTime approvedDate, string anwserContent, string anwserBy, DateTime anwserDate, int isAnswer, int status, DateTime publishedDate, DateTime expiredDate)
		{
			Item entity = new Item();			
			entity.ID = id;
			entity.CategoryID = categoryID;
			entity.Title = title;
			entity.QuesContent = quesContent;
			entity.Name = name;
			entity.Email = email;
			entity.QuesDate = quesDate;
			entity.ApprovedBy = approvedBy;
			entity.ApprovedDate = approvedDate;
			entity.AnwserContent = anwserContent;
			entity.AnwserBy = anwserBy;
			entity.AnwserDate = anwserDate;
			entity.isAnswer = isAnswer;
			entity.Status = status;
			entity.PublishedDate = publishedDate;
			entity.ExpiredDate = expiredDate;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_FAQ_Item_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
			db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, Title);
			db.AddInParameter(dbCommand, "@QuesContent", SqlDbType.NVarChar, QuesContent);
			db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, Name);
			db.AddInParameter(dbCommand, "@Email", SqlDbType.NVarChar, Email);
			db.AddInParameter(dbCommand, "@QuesDate", SqlDbType.DateTime, QuesDate.Year == 1753 ? DBNull.Value : (object) QuesDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.NVarChar, ApprovedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year == 1753 ? DBNull.Value : (object) ApprovedDate);
			db.AddInParameter(dbCommand, "@AnwserContent", SqlDbType.NVarChar, AnwserContent);
			db.AddInParameter(dbCommand, "@AnwserBy", SqlDbType.NVarChar, AnwserBy);
			db.AddInParameter(dbCommand, "@AnwserDate", SqlDbType.DateTime, AnwserDate.Year == 1753 ? DBNull.Value : (object) AnwserDate);
			db.AddInParameter(dbCommand, "@isAnswer", SqlDbType.Int, isAnswer);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.Int, Status);
			db.AddInParameter(dbCommand, "@PublishedDate", SqlDbType.DateTime, PublishedDate.Year == 1753 ? DBNull.Value : (object) PublishedDate);
			db.AddInParameter(dbCommand, "@ExpiredDate", SqlDbType.DateTime, ExpiredDate.Year == 1753 ? DBNull.Value : (object) ExpiredDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<Item> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Item item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteItem(int id)
		{
			Item entity = new Item();
			entity.ID = id;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_FAQ_Item_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<Item> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Item item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}